const db = require('../models'); //引入数据模型模块


class driverservice {
    //通过openId查询用户信息
    async findDriver(openId) {
            let obj = new Object();
            let data = new Object();
            try {
                let resultDriver = await db.der.findOne({
                    where: {
                        openId: openId,
                    },
                    raw: true,
                    attributes: ['DriverID', 'DriverName', 'PhoneNum', 'status', 'FleetID']
                })
                if (resultDriver === null) {
                    obj["code"] = 0;
                    obj["data"] = '';
                    return obj
                }
                //查询车队名称
                let resultFleet = await db.Fleet.findOne({
                    where: {
                        FleetID: resultDriver['FleetID']
                    },
                    raw: true,
                    attributes: ['FleetCName']
                })
                let resultTruck = await db.driverTruck.findOne({
                    where: {
                        DriverID: resultDriver['DriverID']
                    },
                    raw: true,
                    attributes: ['TruckID']
                })
                let resultTruckLic = await db.truck.findOne({
                    where: {
                        TruckID: resultTruck['TruckID']
                    },
                    raw: true,
                    attributes: ["TruckLic"]
                })
                obj["code"] = 1;
                data["DriverID"] = resultDriver["DriverID"];
                data["DriverName"] = resultDriver["DriverName"];
                data["PhoneNum"] = resultDriver["PhoneNum"];
                data["FleetCName"] = resultFleet["FleetCName"];
                data["Status"] = resultDriver["status"];
                data["TruckLic"] = resultTruckLic["TruckLic"];
                obj["data"] = data;
                return obj;
            } catch (error) {
                return { 'code': 0, 'msg': 'database error' };
            }

        }
        //用户退出清空openId
    async updateOpenId(phoneNum) {
            const resultOpenId = await db.der.update({ OpenID: null }, {
                where: {
                    PhoneNum: phoneNum
                }
            })
            if (Number(resultOpenId) === 1) {
                return new Object({ "code": 1 })
            } else {
                return new Object({ "code": 0 })
            }
        }
        //用户手机号验证
    async verifyLogin(params) {
            const resultVerify = await db.der.update({ OpenID: params.openid }, {
                where: {
                    PhoneNum: params.phone
                }
            })
            if (Number(resultVerify) === 1) {
                return new Object({ "code": 1 })
            } else {
                return new Object({ "code": 0 })
            }
        }
        //由车牌获取用户TruckID,DriverId,Drivername,OpenID
    async getDriverId(truckLick) {
        var messageParams = {};
        const resultTruckID = await db.truck.findOne({
            where: {
                TruckLic: truckLick
            },
            attributes: ['TruckID'],
            raw: true
        })
        if (JSON.stringify(resultTruckID) == 'null') {
            return { code: 0 }
        } else {
            const resultDriverID = await db.driverTruck.findOne({
                where: {
                    TruckID: resultTruckID.TruckID
                },
                attributes: ['DriverID'],
                raw: true
            })
            if (JSON.stringify(resultDriverID) == 'null') {
                return { code: 0 }
            } else {
                const resultDriverInfo = await db.der.findOne({
                    where: {
                        DriverID: resultDriverID.DriverID
                    },
                    attributes: ['OpenId', 'DriverID', 'DriverName'],
                    raw: true
                })
                messageParams = {...resultDriverInfo, ...messageParams };
                return messageParams;
            }
        }

    }
}
// const server = new driverservice();
// server.getDriverId('皖BKFT903').then(value => {
//     console.log(value);
// })
//用户信息查询测试
// const server = new driverservice();
// server.findDriver('oPBuf6haO1mR_ASkIFsWdLxobmSE').then(value => {
//     console.log(value);
// })


module.exports = new driverservice();


/**
 * const message = {
    OpenId: 'oPBuf6haO1mR_ASkIFsWdLxobmSE',
    DriverName: '王先生',
    TruckLic: '皖BKFT903',
    ContainerPlace: 'FUBAC2565',
    ContainerID: '25648',
    CreateTime: '2021-12-5 16:38:50',
    CouponStatus: '是',
    Receipt_Num: '686535'
}
 */